package com.doupi.partner.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.net.URI;

/**
 * redis操作管理
 * 
 * @author Super Jens Wang
 *
 * @version 1.0 @ 2016年7月20日 上午4:45:26
 */
public class RedisUtils {
	private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtils.class);
	private JedisPool jedisPool;
	private Jedis jedis;
	private JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

	public RedisUtils(String ip, int port, int database, int maxTotal, int minIdle, int maxIdle, long maxWaitMillis, boolean testOnBorrow, boolean testOnReturn, boolean testWhileIdle,
			int minEvictableIdleTimeMillis, int timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun) {
		jedisPoolConfig.setMaxTotal(maxTotal);
		jedisPoolConfig.setMinIdle(minIdle);
		jedisPoolConfig.setMaxIdle(maxIdle);
		jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
		jedisPoolConfig.setTestOnBorrow(testOnBorrow);
		jedisPoolConfig.setTestOnReturn(testOnReturn);
		jedisPoolConfig.setTestWhileIdle(testWhileIdle);
		jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
		initJedisPool(ip, port, database);
	}

	public RedisUtils(String host, Integer port, Integer database) {
		initJedisPool(host, port, database);
	}

	/**
	 * 初始化链接池
	 * 
	 * @param host
	 *            host 地址
	 * @param port
	 *            端口
	 * @param database
	 *            数据库 0 - databases配置的最大值
	 */
	private void initJedisPool(String host, int port, int database) {
		try {
			URI uri = new URI("redis://" + host + ":" + port + "/" + database);
			jedisPool = new JedisPool(jedisPoolConfig, uri);
		} catch (Throwable e) {
			LOGGER.error("Redis缓存链接失败，host：{}，端口：{}，database：{}", host, port, database, e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获取jedis
	 */
	public Jedis getJedis() {
		if (null == jedis || !jedis.isConnected()) {
			jedis = jedisPool.getResource();
		}
		return jedis;
	}

	public JedisPoolConfig getJedisPoolConfig() {
		return jedisPoolConfig;
	}

	/**
	 * 删除/批量删除
	 * 
	 * @return 被删除 key 的数量
	 */
	public Long delete(String... keys) {
		return getJedis().del(keys);
	}

	/**
	 * 某个key是否存在
	 */
	public Boolean exists(String key) {
		return getJedis().exists(key);
	}

	/**
	 * 设置某一记录的生存时间，过期删除
	 * 
	 * @param key
	 *            设置过期的key
	 * @param seconds
	 *            超时时间，单位：秒
	 * @return 返回受影响数
	 */
	public Long expire(String key, int seconds) {
		return getJedis().expire(key, seconds);
	}

	/**
	 * 设置
	 * 
	 * @param key
	 *            key
	 * @param value
	 *            value
	 * 
	 * @return 操作结果，字符串
	 */
	public String set(String key, String value) {
		return getJedis().set(key, value);
	}

	/**
	 * 设置，并指定过期时间
	 * 
	 * @param key
	 *            key
	 * @param value
	 *            值
	 * @param seconds
	 *            过期时间，单位秒
	 * @return 操作结果，字符串
	 */
	public String setex(String key, String value, int seconds) {
		return getJedis().setex(key, seconds, value);
	}

	/**
	 * 获取值
	 * 
	 * @param key
	 *            key
	 */
	public String get(String key) {
		return getJedis().get(key);
	}
}
